package com.nanfang51g3.ckz.ui.accounting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.nanfang51g3.ckz.R;
import com.nanfang51g3.ckz.adapter.ext.AccountingAdapter;
import com.nanfang51g3.ckz.application.CkzApplication;
import com.nanfang51g3.ckz.domain.AccountingBean;
import com.nanfang51g3.ckz.ui.base.IPresenter;
import com.nanfang51g3.ckz.ui.base.Base2Activity;
import com.nanfang51g3.ckz.ui.webView.PublicWebView;
import com.nanfang51g3.ckz.utils.AppManager;
import com.nanfang51g3.ckz.utils.EasyToast;
import com.nanfang51g3.ckz.utils.KLog;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler2;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * Created by Administrator on 2016/4/19.
 * * 版本1.0.0功能的类，已弃用[HomeAccountingFragment代替]
 */
public class AccountingActivity extends Base2Activity implements /*OnStartDragListener,*/ AccountingContract.View {
    AccountingContract.AccountingActionsListener accountingActionsListener;
    AccountingAdapter accountingAdapter;
    ArrayList<AccountingBean> mIndos = new ArrayList<>();
    @Bind(R.id.accounting_recycler_view)
    RecyclerView accountingRecyclerView;
    @Bind(R.id.accounting_view_frame)
    PtrClassicFrameLayout accountingViewFrame;

    int page = 1;
    int limit = 10;
    boolean more = false;
    String url = "http://ckt.51g3.com/help.php?app=0";

    @Override
    public IPresenter getPresenter() {
        return null;
    }

    @Override
    public int getContentView() {
        return R.layout.accounting;
    }

    @Override
    public boolean isShowLeftIcon() {
        return true;
    }

    @Override
    public boolean isShowRightIcon() {
        return true;
    }

    @Override
    public int setLeftIcon() {
        return R.mipmap.ic_accounting_left;
    }

    @Override
    public int setRightIcon() {
        return R.mipmap.ic_accounting_doubt;
    }

    @Override
    public int showTitle() {
        return R.string.str_accounting;
    }

    @Override
    public void onClickLeft(View v) {
        AppManager.getAppManager().finishActivity(this);
    }


    @Override
    public void showSuccess(List<AccountingBean> infos, String all_total, String deal_total) {
        if (more) {
            mIndos.addAll(infos);
            accountingAdapter.addList(mIndos);
        } else {
            mIndos.clear();
            AccountingBean accountingBean = new AccountingBean();
            accountingBean.setLayoutType(0);
            accountingBean.setAll_total(all_total);
            accountingBean.setDeal_total(deal_total);
            mIndos.add(0, accountingBean);
            mIndos.addAll(infos);
            accountingAdapter.addList(mIndos);
        }

    }

    @Override
    public void onClickRight(View v) {
        startActivity(new Intent(this, PublicWebView.class).putExtra("id_url", url).putExtra("id_title", R.string.str_wen_da));
    }

    @Override
    public void showProgress() {
        super.showPro(R.string.str_progress_accounting);
    }

    @Override
    public void dismissProgress() {
        super.dismissPro();

    }

    @Override
    public void showMessage(String message) {
        EasyToast.showShort(this, message);
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        accountingActionsListener = DaggerAccountingContractComponent
                .builder()
                .appComponent(CkzApplication.getAppComponent())
                .accountingPresenterModule(new AccountingPresenterModule(this))
                .build().getAccountingPresenter();
        KLog.i("TAG", accountingActionsListener == null ? "accountingActionsListener==null" : "accountingActionsListener!=null");
        ButterKnife.bind(this);
        AccountingBean accountingBean = new AccountingBean();
        accountingBean.setLayoutType(0);
        accountingBean.setAll_total("0");
        accountingBean.setDeal_total("0");
        mIndos.add(0, accountingBean);
        accountingViewFrame.setLastUpdateTimeRelateObject(this);

        accountingViewFrame.setPtrHandler(new PtrDefaultHandler2() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                // TODO Auto-generated method stub
                more = false;
                page = 1;

                /**
                 * @see AccountingPresenter#adList(int, int)
                 */
                accountingActionsListener.adList(page, limit);
                accountingViewFrame.refreshComplete();
            }

            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                // TODO Auto-generated method stub
                more = true;
                ++page;
                /**
                 * @see AccountingPresenter#adList(int, int)
                 */
                accountingActionsListener.adList(page, limit);
                accountingViewFrame.refreshComplete();
            }

        });
        // the following are default settings
        accountingViewFrame.setResistance(1.7f);
        accountingViewFrame.setRatioOfHeaderHeightToRefresh(1.2f);
        accountingViewFrame.setDurationToClose(200);
        accountingViewFrame.setDurationToCloseHeader(1000);
        // default is false
        accountingViewFrame.setPullToRefresh(false);
        // default is true
        accountingViewFrame.setKeepHeaderWhenRefresh(true);
        accountingAdapter = new AccountingAdapter(mIndos/*, this*/);
        accountingAdapter.setActivity(this);
        accountingRecyclerView.setHasFixedSize(true);//固定大小
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        accountingRecyclerView.setLayoutManager(linearLayoutManager);
        //间隔线条
//        heSuanRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        accountingRecyclerView.setAdapter(accountingAdapter);


        /**
         * @see AccountingPresenter#adList(int, int)
         */
        accountingActionsListener.adList(page, limit);
//        //拽拉列表控件
//        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(accountingAdapter);
//        mItemTouchHelper = new ItemTouchHelper(callback);
//        mItemTouchHelper.attachToRecyclerView(heSuanRecyclerView);

    }


//    @Override
//    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
//        mItemTouchHelper.startDrag(viewHolder);
//    }

//    ItemTouchHelper mItemTouchHelper;
}
